﻿using UnityEngine;
using System.Collections;

public class PlayerControls : MonoBehaviour {

	// pelaajan nopeus: ShipModuleData.engines - (ShipModuleData.hullPlating/2)
	public static float move_speed; //player linear movement speed
	public static float turn_speed; //player angular (turning) speed
	public static float speedo; //speedometer

	// Update is called once per frame
	void Update () {

		speedo = Mathf.Round(rigidbody2D.velocity.magnitude * 100.0f) / 10.0f;

		if (GameManager.player_destroyed == true){
			ShipModuleData.enginesON = false;
			return;
		}

		if (ShipModuleData.enginesON == false) {
			move_speed = 0f;
			turn_speed = 0f;
		} else {
			move_speed = ShipModuleData.enginesLv - (ShipModuleData.hullPlatingLv / 2);
			turn_speed = move_speed / 5;
		}

		if (ShipModuleData.dampenersON == true) {
			rigidbody2D.drag = 0.25f;
			rigidbody2D.angularDrag = 0.25f;
		} else {
			rigidbody2D.drag = 0f;
			rigidbody2D.angularDrag = 0f;
		}

		/*if (ShipModuleData.gyroON == false) {
				turn_speed = 0f;
		} else {
				turn_speed = ShipModuleData.gyroLv - (ShipModuleData.hullPlatingLv / 2);
		}*/

		//returning if game is paused
		if (PauseMenu.paused == true)
			return;

		// vasen
		if (Input.GetKey (KeyCode.A)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddForce (-transform.right * move_speed);
		}

		// oikea
		if (Input.GetKey (KeyCode.D)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddForce (transform.right * move_speed);
		}

		// eteen
		if (Input.GetKey (KeyCode.W)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddForce (transform.up * move_speed);
		}

		// taakse
		if (Input.GetKey (KeyCode.S)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddForce (-transform.up * move_speed);
		}

		// käännetään alusta vasemmalle
		if (Input.GetKey (KeyCode.Q)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddTorque (turn_speed);
		}
		// käännetään alusta oikealle
		if (Input.GetKey (KeyCode.E)) {
				// lisätään voimaa rigidbodyyn
				rigidbody2D.AddTorque (-turn_speed);
		}
		// engines on/off
	/*	if (Input.GetKeyDown (KeyCode.X))
				 ShipModuleData.enginesON;
	 */
		} 
		
	void OnCollisionEnter2D(Collision2D coll)
	{
		if (GameManager.playerSP <= 0) {
			if (coll.gameObject.tag == "Enemy1")
				GameManager.playerHP -= (WeaponsData.enemyCollisionHPdmg - ShipModuleData.hullPlatingLv);
			if (coll.gameObject.tag == "stellarObject")
				GameManager.playerHP -= (WeaponsData.stellarCollisionHPdmg - ShipModuleData.hullPlatingLv);
			if (coll.gameObject.tag == "projectile1")
				GameManager.playerHP -= (WeaponsData.projectile1HPdmg - ShipModuleData.hullPlatingLv);
			if (coll.gameObject.tag == "solarObject")
				GameManager.playerHP -= (WeaponsData.solarCollisionHPdmg);
		}
		if (GameManager.playerSP > 0) {
			if (coll.gameObject.tag == "Enemy1")
				GameManager.playerSP -= WeaponsData.enemyCollisionSPdmg;
			if (coll.gameObject.tag == "stellarObject")
				GameManager.playerSP -= WeaponsData.stellarCollisionSPdmg;
			if (coll.gameObject.tag == "projectile1")
				GameManager.playerSP -= WeaponsData.projectile1SPdmg;
			if (coll.gameObject.tag == "solarObject") {
				GameManager.playerHP -= WeaponsData.solarCollisionHPdmg;
				GameManager.playerSP -= WeaponsData.solarCollisionSPdmg;
			}
		}

		/* if (GameManager.playerSP <= 0) {
			GameManager.playerHP = GameManager.playerHP - (WeaponsData.collisionHPdmg - ShipModuleData.hullPlatingLv); // reducing hull points
		} else {
			GameManager.playerSP = GameManager.playerSP - (WeaponsData.collisionSPdmg); // reducing shield points
		} */
		
	}
}